<template>
  <div class="edit-box">
    <el-form :model="form" :inline="true" label-position="top">
      <el-form-item label="登录账号">
        <el-input v-model="form.userid"></el-input>
      </el-form-item>
      <el-form-item label="姓名">
        <el-input v-model="form.userName"></el-input>
      </el-form-item>
      <el-form-item label="手机号">
        <el-input v-model="form.phoneNumber"></el-input>
      </el-form-item>
      <el-form-item label="邮箱">
        <el-input v-model="form.emailAddress"></el-input>
      </el-form-item>
      <el-form-item label="所属部门">
        <el-select v-model="form.deptId" multiple placeholder="请选择">
          <el-option v-for="item in deptList" :key="item.id" :label="item.deptName" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item style="width:100%">
        <el-checkbox v-model="form.delState" :true-label="0" :false-label="1">是否启用</el-checkbox>
      </el-form-item>
      <el-form-item label="所属角色">
        <el-select v-model="form.roleId" multiple placeholder="请选择">
          <el-option v-for="item in roleList" :key="item.id" :label="item.roleName" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="$emit('changeState',false)">取 消</el-button>
      <el-button type="primary" @click="submit">保存</el-button>
    </div>
  </div>
</template>
<script>
import { update, findById } from '@/api/user'
import { findList as deptList } from '@/api/dept'
import { findList as roleList } from '@/api/role'
export default {
  data() {
    return {
      dialogVisible: false,
      form: {
        companyId: this.$store.state.user.companyId,
        customerType: '',
        userid: '',
        userName: '',
        userPwd: '',
        phoneNumber: '',
        emailAddress: '',
        delState: 0,
        deptId: [],
        roleId: []
      },
      deptList: [],
      roleList: []
    }
  },
  beforeDestroy() {
    this.$off('reload')
    this.$off('changeState')
  },
  methods: {
    initDialogData(id) {
      this.initDeptList()
      this.initRoleList()
      findById(id).then(res => {
        this.form = res.data
      })
    },
    initDeptList() {
      deptList(this.form.companyId).then(res => {
        this.deptList = res.data
      })
    },
    initRoleList() {
      roleList(this.form.companyId).then(res => {
        this.roleList = res.data
      })
    },
    submit() {
      update(this.form).then(res => {
        if (res.success) {
          this.$emit('reload')
          this.$emit('changeState', false)
          this.$message.success(res.message)
        } else {
          this.$message.error(res.message)
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.edit-box {
  padding: 0 50px;
  height: 550px;
  overflow-y: auto;
}
.dialog-footer {
  text-align: right;
}
.el-form--inline .el-form-item {
  width: calc(50% - 12px);
}
</style>
